package com.zl.customer.web;

import com.zl.customer.pojo.Customer;
import com.zl.customer.pojo.CustomerCondition;
import com.zl.customer.pojo.Page;
import com.zl.customer.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@Controller
@RequestMapping("/customer")  //窄化请求,  该Controller类中所有的处理器方法url必须添加该前缀
//@ResponseBody

//@RestController  // @Controller 与 /@ResponseBody 综合注解
public class CustomerController {
    @Autowired
    private CustomerService customerService;


    //表示该方法只能处理get请求,
    //@RequestMapping(value="/queryAll",method= RequestMethod.GET)
    //@GetMapping("/queryAll")
    //@PostMapping("/queryAll")
    /*public ModelAndView queryAll(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("customers",customerService.queryAll());
        modelAndView.setViewName("list");
        return modelAndView;
    }*/
    @GetMapping("/queryAll")
    public String queryAll(Model model) throws Exception {
        //把查询到数据保存到request域
        model.addAttribute("customers", customerService.queryAll());
        return "list";
    }

    @PostMapping("/addCustomer")
    public String addCustomer(Customer customer, Model model, MultipartFile upload) throws Exception {


        //upload.getOriginalFilename(); //得到上传文件名
        //upload.getName();  //得到请求参数的key

        //upload.transferTo(File ); //保存文件
        if (upload != null) {
            //产生一个文件名
            //1.时间戳
           //1. new  Date().getTime()  得到当前时间的毫秒值
            // 2. System.currentTimeMillis()
            //long timer = System.currentTimeMillis();

            //2.使用UUID生成文件名
            String uuid = UUID.randomUUID().toString().toUpperCase().replace("-","");
            String uplodaName = upload.getOriginalFilename();
            //得到后缀名
            String  suffix =uplodaName.substring(uplodaName.lastIndexOf("."));
            //保存的文件名
            String saveName = uuid+suffix;
            //保存文件
            File file = new File("D:/customer",saveName);
            upload.transferTo(file);
            //给customer对象的photo属性赋值
            customer.setPhoto(saveName);
        }

        int rs = customerService.addCustomer(customer);
        if (rs > 0) {
            model.addAttribute("msg", "添加成功");
        } else {
            model.addAttribute("msg", "添加失败");
        }
        return "msg";
    }


    @RequestMapping("/queryCustomerByCondition")
    public String queryCustomerByCondition(CustomerCondition condition, Model model) throws Exception {
        Page<Customer> customerPage = customerService.queryCondition(condition);
        model.addAttribute("pages", customerPage);
        model.addAttribute("condition", condition);

        return "list2";
    }


    //deleteByIds
    //如果前端传递的一个key对应多个值,  ids=1&ids=2  Controller的方法使用数组类型参数接收
    //如果想使用集合来接收, 把该集合参数作为包装类的属性
    @GetMapping("/deleteByIds")
    //public String deleteByIds(CustomerCondition condition){
    public String deleteByIds(Integer[] ids) throws Exception {
        //作业   delete from tb_customer where  cid = 1 or  cid = 2
        customerService.deleteById(null);
        return "redirect:/customer/queryCustomerByCondition.do"; //get
    }

  ///queryById/5
    @RequestMapping("/queryById/{cid}")  // 默认找视图  把Customer对象转换json格式  只需要加 @ResponseBody
    //表示该方法的返回值 作为一个响应体(数据) 响应给前端
    @ResponseBody
    public Customer  queryById(@PathVariable("cid") Integer id)throws Exception{
        return customerService.queryOne(id);
    }

}
